//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics;
using System.Diagnostics.Contracts;

namespace OpenLS.Spreadsheet.Expressions
{

    class StaticCellReference: CellReference
    {
        private StaticCellReference(RowReference row, ColumnReference column)
        {
            _row = row;
            _column = column;
        }
        public StaticCellReference(FloatingRowReference row, FloatingColumnReference column)
        {
            _row = row;
            _column = column;
        }
        public StaticCellReference(AnchoredRowReference row, AnchoredColumnReference column)
        {
            _row = row;
            _column = column;
        }
        internal override CellReference ToggleLock()
        {
            AnchoredRowReference rr = (AnchoredRowReference) Row;
            AnchoredColumnReference cr = (AnchoredColumnReference) Column;
            if (rr.IsLocked)
            {
                if (cr.IsLocked)
                {
                    cr = new AnchoredColumnReference(cr.Anchor, false);
                }
                else
                {
                    rr = new AnchoredRowReference(rr.Anchor, false);
                    cr = new AnchoredColumnReference(cr.Anchor, true);
                }
            }
            else
            {
                if (cr.IsLocked)
                    cr = new AnchoredColumnReference(cr.Anchor, false);
                else
                {
                    cr = new AnchoredColumnReference(cr.Anchor, true);
                    rr = new AnchoredRowReference(rr.Anchor, true);
                }
            }
            return new StaticCellReference(rr, cr);
        }

        internal override CellReference Projected(Worksheet targetWorksheet)
        {
            return new StaticCellReference(Row.Projected(targetWorksheet), Column.Projected(targetWorksheet));
        }

        readonly RowReference _row;
        readonly ColumnReference _column;

        internal override RowReference Row
        {
            get { return _row; }
        }

        internal override ColumnReference Column
        {
            get { return _column; }
        }
        public override Worksheet Worksheet { get { return Row.Worksheet; } }
        public override Cell Cell
        {
            get
            {
                return Worksheet[Address];
            }
        }
        public CellAddress Address2
        {
            get
            {
                return new CellAddress(Row.Anchor.Index, Column.Anchor.Index);
            }
        }
        [Obsolete]
        public override CellReference Offset(CellRelativeAddress a)
        {
            ColumnReference c = Column.Offset(a);
            RowReference r = Row.Offset(a);
            if (c == Column && r == Row)
                return this;
            return new StaticCellReference(r, c);
            
        }

    }
}
